package com.study.hot.jetpack.navigation.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.study.hot.R
import com.study.hot.databinding.FragmentGoodsListBinding
import com.study.hot.jetpack.navigation.GoodsViewModel
import kotlinx.android.synthetic.main.fragment_goods_list.*
import org.jetbrains.anko.sdk27.coroutines.onClick


class GoodsListFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        val binding = DataBindingUtil.inflate<FragmentGoodsListBinding>(inflater, R.layout.fragment_goods_list, container, false)
        val viewModel = ViewModelProvider(requireActivity())[GoodsViewModel::class.java]
        binding.data = viewModel
        binding.lifecycleOwner = activity

        binding.seekBar.progress = viewModel.numbers.value!!
        binding.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                viewModel.numbers.value = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }

        })
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        goDetailBtn.onClick { v ->
            //找到该button对应的Controller
            val findNavController = Navigation.findNavController(v!!)
            //跳转并携带参数
            val bundle = Bundle()
            bundle.putString("id", "a001")
            findNavController.navigate(R.id.action_goodsListFragment_to_goodsDetailFragment, bundle)
        }
    }
}
